/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.home.communities;

import java.io.StringReader;
import java.util.Vector;

import org.xml.sax.InputSource;

import br.ufes.cwtools.joinus.home.Home2;
import br.ufes.cwtools.joinus.util.ServerCom;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import br.ufes.cwtools.joinus.R;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CommuShow {
	
	final SQLiteDatabase myBase;
	final String user;
	
	public CommuShow(final long id, final Activity a,SQLiteDatabase myBase, String user){ 
		this.myBase=myBase;
		this.user=user;
		((Home2)a).setPreviousScreen(2);
		Log.d("DEBUG","getting community data");
		String xml = ServerCom.gtCommu(id+"");
		
		InputSource is = new InputSource();
		is.setCharacterStream(new StringReader(xml));
		Commu factory = new Commu();
		Vector<Commu> commu = factory.fromXml(is);
		final Commu toShow = commu.get(0);
		
		a.setContentView(R.layout.create_commu);
		final EditText name=(EditText)a.findViewById(R.id.create_commu_edit_name), 
				desc=(EditText)a.findViewById(R.id.create_commu_edit_desc);
		Log.d("COMP",toShow.getOwnerName()+'/'+ServerCom.getMyId());
		if(toShow.getOwnerName().compareTo(ServerCom.getMyId())==0){
			name.setEnabled(true);
			desc.setEnabled(true);
			Button Edit = (Button)a.findViewById(R.id.create_commu_edit_but2);
			Edit.setVisibility(View.VISIBLE);
			Edit.setOnClickListener(new OnClickListener(){
				public void onClick(View arg0) {
					ServerCom.editComu(toShow.getId(), name.getText().toString(), desc.getText().toString(), 0);
				}
			});
		}else{
			name.setEnabled(false);
			desc.setEnabled(false);
		}
		name.setText(toShow.getCommuName());
		desc.setText(toShow.getDes());
		
		Button ViewMembers = (Button)a.findViewById(R.id.create_commu_edit_but);
		ViewMembers.setText("View Members");
		ViewMembers.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				new MembersList(id, a, CommuShow.this.myBase, CommuShow.this.user);
			}
		});
		

		
	}
}
